package com.puppet.building182.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.puppet.building182.pojo.Building;
import com.puppet.building182.pojo.MyPageData;
import com.puppet.building182.pojo.Result;
import com.puppet.building182.pojo.VO.BuildingVo;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/build")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @GetMapping(value = {"/{pageNum}/{pageSize}", "/{pageNum}/{pageSize}/{search}"})
    public Result<?> getAllBuildings(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @PathVariable(value = "search", required = false)  String search) {
        PageHelper.startPage(pageNum, pageSize);
        List<Building> buildings = buildingService.getAllBuildings(search);
        PageInfo<Building> info = new PageInfo<>(buildings);
        MyPageData<Building> myPageData = new MyPageData<>(info.getList(), info.getTotal());
        return Result.ok(myPageData);
    }

    @PostMapping
    public Result<?> addBuilding(@RequestBody Building building) {
        return Result.ok(buildingService.addBuilding(building), BuildingCode.SAVE_SUCCESS.getMessage());
    }

    @DeleteMapping("/{buildingId}")
    public Result<?> deleteBuildingById(@PathVariable("buildingId") Integer buildingId) {
        return Result.ok(buildingService.deleteBuildingById(buildingId), BuildingCode.DELETE_SUCCESS.getMessage());
    }

    @PostMapping("/deleteBuildings")
    public Result<?> deleteBuildings(@RequestBody List<Integer> buildings) {
        return Result.ok(buildingService.deleteBuildings(buildings), BuildingCode.DELETE_SUCCESS.getMessage());
    }
    @PutMapping
        public Result<?> updateBuilding(@RequestBody Building building) {
        return Result.ok(buildingService.updateBuilding(building),BuildingCode.SAVE_SUCCESS.getMessage());
    }
    @GetMapping("/getBuildingById/{id}")
    public Result<?> getBuildingById(@PathVariable("id") Integer id) {
        return Result.ok(buildingService.getBuildingById(id));
    }
    @GetMapping("/getBuildingNameAndId")
    public Result<?> getBuildingNameAndId() {
        return Result.ok(buildingService.getBuildingNameAndId());
    }
}
